<?php
class ModelAccountOrder extends Model {
	public function getOrder($order_id) {

	$order_query = $this->db->query("SELECT o.*, os.name as status  FROM `" . DB_PREFIX . "order` as o LEFT JOIN " . DB_PREFIX . "order_status as os ON (o.order_status_id = os.order_status_id) WHERE order_id = '" . (int)$order_id . "' AND customer_id = '" . (int)$this->customer->getId() ."'");

		if ($order_query->num_rows) {
			$country_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "country` WHERE country_id = '" . (int)$order_query->row['payment_country_id'] . "'");

			if ($country_query->num_rows) {
				$payment_iso_code_2 = $country_query->row['iso_code_2'];
				$payment_iso_code_3 = $country_query->row['iso_code_3'];
			} else {
				$payment_iso_code_2 = '';
				$payment_iso_code_3 = '';
			}

			$zone_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "zone` WHERE zone_id = '" . (int)$order_query->row['payment_zone_id'] . "'");

			if ($zone_query->num_rows) {
				$payment_zone_code = $zone_query->row['code'];
			} else {
				$payment_zone_code = '';
			}

			$country_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "country` WHERE country_id = '" . (int)$order_query->row['shipping_country_id'] . "'");

			if ($country_query->num_rows) {
				$shipping_iso_code_2 = $country_query->row['iso_code_2'];
				$shipping_iso_code_3 = $country_query->row['iso_code_3'];
			} else {
				$shipping_iso_code_2 = '';
				$shipping_iso_code_3 = '';
			}

			$zone_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "zone` WHERE zone_id = '" . (int)$order_query->row['shipping_zone_id'] . "'");

			if ($zone_query->num_rows) {
				$shipping_zone_code = $zone_query->row['code'];
			} else {
				$shipping_zone_code = '';
			}

			return array(
				'order_id'                => $order_query->row['order_id'],
				'invoice_no'              => $order_query->row['invoice_no'],
				'invoice_prefix'          => $order_query->row['invoice_prefix'],
				'store_id'                => $order_query->row['store_id'],
				'store_name'              => $order_query->row['store_name'],
				'store_url'               => $order_query->row['store_url'],
				'customer_id'             => $order_query->row['customer_id'],
				'firstname'               => $order_query->row['firstname'],
				'lastname'                => $order_query->row['lastname'],
				'telephone'               => $order_query->row['telephone'],
				'fax'                     => $order_query->row['fax'],
				'email'                   => $order_query->row['email'],
				'payment_firstname'       => $order_query->row['payment_firstname'],
				'payment_lastname'        => $order_query->row['payment_lastname'],
				'payment_company'         => $order_query->row['payment_company'],
				'payment_address_1'       => $order_query->row['payment_address_1'],
				'payment_address_2'       => $order_query->row['payment_address_2'],
				'payment_postcode'        => $order_query->row['payment_postcode'],
				'payment_city'            => $order_query->row['payment_city'],
				'payment_zone_id'         => $order_query->row['payment_zone_id'],
				'payment_zone'            => $order_query->row['payment_zone'],
				'payment_zone_code'       => $payment_zone_code,
				'payment_country_id'      => $order_query->row['payment_country_id'],
				'payment_country'         => $order_query->row['payment_country'],
				'payment_iso_code_2'      => $payment_iso_code_2,
				'payment_iso_code_3'      => $payment_iso_code_3,
				'payment_address_format'  => $order_query->row['payment_address_format'],
				'payment_method'          => $order_query->row['payment_method'],
				'shipping_firstname'      => $order_query->row['shipping_firstname'],
				'shipping_lastname'       => $order_query->row['shipping_lastname'],
				'shipping_company'        => $order_query->row['shipping_company'],
				'shipping_address_1'      => $order_query->row['shipping_address_1'],
				'shipping_address_2'      => $order_query->row['shipping_address_2'],
				'shipping_postcode'       => $order_query->row['shipping_postcode'],
				'shipping_city'           => $order_query->row['shipping_city'],
				'shipping_zone_id'        => $order_query->row['shipping_zone_id'],
				'shipping_zone'           => $order_query->row['shipping_zone'],
				'shipping_zone_code'      => $shipping_zone_code,
				'shipping_country_id'     => $order_query->row['shipping_country_id'],
				'shipping_country'        => $order_query->row['shipping_country'],
				'shipping_iso_code_2'     => $shipping_iso_code_2,
				'shipping_iso_code_3'     => $shipping_iso_code_3,
				'shipping_address_format' => $order_query->row['shipping_address_format'],
				'shipping_method'         => $order_query->row['shipping_method'],
				'comment'                 => $order_query->row['comment'],
				'total'                   => $order_query->row['total'],
                'shipping_district'       =>$order_query->row['shipping_district'],
				'order_status_id'         => $order_query->row['order_status_id'],
				'language_id'             => $order_query->row['language_id'],
				'currency_id'             => $order_query->row['currency_id'],
				'currency_code'           => $order_query->row['currency_code'],
				'currency_value'          => $order_query->row['currency_value'],
				'date_modified'           => $order_query->row['date_modified'],
				'date_added'              => $order_query->row['date_added'],
				'ip'                      => $order_query->row['ip'],
			    'status'                  => $order_query->row['status']
			);
		} else {
			return false;
		}
	}

	public function getOrderBySystem($order_id) {
	    $order_query = $this->db->query("SELECT o.*,os.name as status,c.firstname as customer_name,c.image as customer_image,s.rank as cooker_rank,s.firstname as cooker_nickname,s.lastname as cooker_name,s.image as cooker_image,s.email as cooker_contact,s.customer_id as cooker_id FROM `" . DB_PREFIX . "order` o LEFT JOIN customer c ON (c.customer_id=o.customer_id) LEFT JOIN customer s ON (s.customer_id=o.product_customer_id) LEFT JOIN " . DB_PREFIX . "order_status os ON (o.order_status_id = os.order_status_id)  WHERE order_id = '" . (int)$order_id . "'");

	    if ($order_query->num_rows) {
	        return array(
	            'order_id'                => $order_query->row['order_id'],
	            'status'                => $order_query->row['status'],
	            'order_status_id'                => $order_query->row['order_status_id'],
	            'invoice_no'              => $order_query->row['invoice_no'],
	            'invoice_prefix'          => $order_query->row['invoice_prefix'],
	            'store_id'                => $order_query->row['store_id'],
	            'store_name'              => $order_query->row['store_name'],
	            'store_url'               => $order_query->row['store_url'],
	            'customer_id'             => $order_query->row['customer_id'],
	            'firstname'               => $order_query->row['firstname'],
	            'lastname'                => $order_query->row['lastname'],
	            'telephone'               => $order_query->row['telephone'],
	            'fax'                     => $order_query->row['fax'],
	            'email'                   => $order_query->row['email'],
	            'payment_method'          => $order_query->row['payment_method'],
	            'comment'                 => $order_query->row['comment'],
	            'total'                   => $order_query->row['total'],
	            'order_status_id'         => $order_query->row['order_status_id'],
	            'language_id'             => $order_query->row['language_id'],
	            'currency_id'             => $order_query->row['currency_id'],
	            'currency_code'           => $order_query->row['currency_code'],
	            'currency_value'          => $order_query->row['currency_value'],
	            'date_modified'           => $order_query->row['date_modified'],
	            'date_added'              => $order_query->row['date_added'],
	            'ip'                      => $order_query->row['ip'],
	            'customer_name'                      => $order_query->row['customer_name'],
	            'customer_image'                      => $order_query->row['customer_image'],
	            'cooker_name'                      => $order_query->row['cooker_name'],
	            'cooker_rank'                      => $order_query->row['cooker_rank'],
	            'cooker_nickname'                      => $order_query->row['cooker_nickname'],
	            'cooker_image'                      => $order_query->row['cooker_image'],
	            'cooker_contact'                      => $order_query->row['cooker_contact'],
	            'cooker_id'                      => $order_query->row['cooker_id'],
	        );
	    } else {
	        return false;
	    }
	}

	public function getOrderProductAddress($order_id, $order_product_id) {

	    $query = $this->db->query("SELECT a.* FROM " . DB_PREFIX . "order_product op LEFT JOIN product p ON (op.product_id = p.product_id) LEFT JOIN address a ON (a.address_id = p.product_address_id) WHERE order_id = '" . (int)$order_id . "' AND order_product_id = '" . (int)$order_product_id . "'");

	    $country_query = $this->db->query("SELECT name FROM `" . DB_PREFIX . "country` WHERE country_id = '" . (int)$query->row['country_id'] . "'");

	    if ($country_query->num_rows) {
	        $country = $country_query->row['name'];
	    } else {
	        $country = '';
	    }

	    $zone_query = $this->db->query("SELECT name FROM `" . DB_PREFIX . "zone` WHERE zone_id = '" . (int)$query->row['zone_id'] . "'");

	    if ($zone_query->num_rows) {
	        $zone = $zone_query->row['name'];
	    } else {
	        $zone = '';
	    }

	    $district_query = $this->db->query("SELECT name FROM `" . DB_PREFIX . "district` WHERE district_id = '" . (int)$query->row['district_id'] . "'");

	    if ($district_query->num_rows) {
	        $district = $zone_query->row['name'];
	    } else {
	        $district = '';
	    }

	    return array(
	        'country'          => $country,
	        'zone'             => $zone,
	        'district'         => $district,
	        'address'          => $query->row['address_1'],
	        'lat'          => $query->row['lat'],
	        'lng'          => $query->row['lng'],
	    );

	}

	public function getOrders($start = 0, $limit = 20) {
		if ($start < 0) {
			$start = 0;
		}

		if ($limit < 1) {
			$limit = 1;
		}

		$query = $this->db->query("SELECT o.order_id, o.firstname, o.lastname, os.name as status, o.order_status_id, o.date_added, o.total, o.currency_code, o.currency_value FROM `" . DB_PREFIX . "order` o LEFT JOIN " . DB_PREFIX . "order_status os ON (o.order_status_id = os.order_status_id) WHERE o.customer_id = '" . (int)$this->customer->getId() ."' ORDER BY o.order_id DESC LIMIT " . (int)$start . "," . (int)$limit);

		return $query->rows;
	}

	public function getSaleOrders($start = 0, $limit = 20) {
	    if ($start < 0) {
	        $start = 0;
	    }

	    if ($limit < 1) {
	        $limit = 1;
	    }
	    $waitpay_order_status = $this->config->get("config_order_status_id");

	    $query = $this->db->query("SELECT o.order_id, o.firstname, o.lastname, os.name as status, o.order_status_id, o.date_added, o.total, o.currency_code, o.currency_value FROM `" . DB_PREFIX . "order` o LEFT JOIN " . DB_PREFIX . "order_status os ON (o.order_status_id = os.order_status_id)  WHERE o.customer_id = '" . (int)$this->customer->getId() . "' AND o.order_status_id <> '".(int)$waitpay_order_status."' AND o.order_status_id > '0' ORDER BY o.order_id DESC LIMIT " . (int)$start . "," . (int)$limit);

	    return $query->rows;
	}

	public function getOrderProduct($order_id, $order_product_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_product WHERE order_id = '" . (int)$order_id . "' AND order_product_id = '" . (int)$order_product_id . "'");

		return $query->row;
	}

	public function getOrderProducts($order_id) {
		$query = $this->db->query("SELECT op.order_product_id,op.order_id,op.product_id,op.name,op.model,op.quantity,op.price,op.total,op.tax,op.reward,op.orginal_price,p.image,p.likes,p.points FROM " . DB_PREFIX . "order_product as op left join " .DB_PREFIX. "product as p on op.product_id = p.product_id  WHERE order_id = '" . (int)$order_id . "'");

		return $query->rows;
	}

	public function getOrderOptions($order_id, $order_product_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_option WHERE order_id = '" . (int)$order_id . "' AND order_product_id = '" . (int)$order_product_id . "'");

		return $query->rows;
	}

	public function getOrderTimeOption($order_id, $order_product_id) {
	    $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_option WHERE order_id = '" . (int)$order_id . "' AND order_product_id = '" . (int)$order_product_id . "'");

	    return $query->row;
	}

	public function getOrderVouchers($order_id) {
		$query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "order_voucher` WHERE order_id = '" . (int)$order_id . "'");

		return $query->rows;
	}

	public function getOrderInfo($order_id) {

		$query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "order` WHERE order_id = '" . (int)$order_id . "'");

		return $query->row;
	}

	public function getOrderHistories($order_id) {
		$query = $this->db->query("SELECT date_added, os.name AS status, oh.comment, oh.notify FROM " . DB_PREFIX . "order_history oh LEFT JOIN " . DB_PREFIX . "order_status os ON oh.order_status_id = os.order_status_id WHERE oh.order_id = '" . (int)$order_id . "' ORDER BY oh.date_added");

		return $query->rows;
	}

	public function getUnReviewTotalOrders() {
	    $implode = array();

	    $order_statuses = $this->config->get('config_complete_status');

	    foreach ($order_statuses as $order_status_id) {
	        $implode[] = "o.order_status_id = '" . (int)$order_status_id . "'";
	    }

	    if ($implode) {
	        $query = $this->db->query("SELECT count(1) AS total FROM `" . DB_PREFIX . "order` o LEFT JOIN " . DB_PREFIX . "review r on (o.order_id=r.order_id)  WHERE o.product_customer_id = '" . (int)$this->customer->getId() . "' AND (" . implode(" OR ", $implode) . ") AND r.review_id is null");

	        return $query->row['total'];
	    } else {
	        return 0;
	    }
	}

	public function getTotalOrders() {
	    $waitpay_order_status = $this->config->get("config_order_status_id");
		$query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "order` o WHERE customer_id = '" . (int)$this->customer->getId() . "' AND o.order_status_id <> '".(int)$waitpay_order_status."' AND o.order_status_id > '0'");

		return $query->row['total'];
	}

	public function getSaleTotalOrders() {

	    $waitpay_order_status = $this->config->get("config_order_status_id");

	    $query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "order` o WHERE product_customer_id = '" . (int)$this->customer->getId() . "'  AND o.order_status_id <> '".(int)$waitpay_order_status."' AND o.order_status_id > '0'");

	    return $query->row['total'];
	}



	public function getCustomerSaleTotalOrders($customer_id) {

	    $waitpay_order_status = $this->config->get("config_order_status_id");

	    $query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "order` o WHERE o.customer_id = '" . (int)$customer_id . "'  AND o.order_status_id <> '".(int)$waitpay_order_status."' AND o.order_status_id > '0'");

	    return $query->row['total'];
	}

	public function getTotalOrderProductsByOrderId($order_id) {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "order_product WHERE order_id = '" . (int)$order_id . "'");

		return $query->row['total'];
	}

	public function getTotalOrderVouchersByOrderId($order_id) {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "order_voucher` WHERE order_id = '" . (int)$order_id . "'");

		return $query->row['total'];
	}

	public function getSaleTotalMoney() {

	    $implode = array();

	    $order_statuses = $this->config->get('config_complete_status');
	    $order_pstatuses = $this->config->get('config_processing_status');

	    foreach ($order_statuses as $order_status_id) {
	        $implode[] = "order_status_id = '" . (int)$order_status_id . "'";
	    }

	    foreach ($order_pstatuses as $order_status_id) {
	        $implode[] = "order_status_id = '" . (int)$order_status_id . "'";
	    }

	    if ($implode) {
	        $query = $this->db->query("SELECT SUM(total) AS total FROM `" . DB_PREFIX . "order` WHERE product_customer_id = '" . (int)$this->customer->getId() . "' AND (" . implode(" OR ", $implode) . ")");

	        return $query->row['total'];
	    } else {
	        return 0;
	    }

	}

	public function getSaleTotalFreezeMoney() {
	    $implode = array();

	    $order_statuses = $this->config->get('config_processing_status');

	    foreach ($order_statuses as $order_status_id) {
	        $implode[] = "order_status_id = '" . (int)$order_status_id . "'";
	    }

	    if ($implode) {
	        $query = $this->db->query("SELECT SUM(total) AS total FROM `" . DB_PREFIX . "order` WHERE  product_customer_id = '" . (int)$this->customer->getId() . "' AND (" . implode(" OR ", $implode).")");

	        return $query->row['total'];
	    } else {
	        return 0;
	    }
	}
	/**
	 * @param //1:小计  2:总计   3:折扣   4:秒杀  5:使用券  6:运费 $type
	 */
	public function getOrderTotalByType($order_id,$type){
	    //秒杀
	    if($type==4){
	        $order_total = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_total WHERE order_id = '" . (int)$order_id . "'and type = 4 ")->row;
	    }
	    //使用券
	    if($type==5){
	        $order_total = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_total WHERE order_id = '" . (int)$order_id . "'and type = 5 ")->row;
	    }
	    return $order_total;
	}

	//获取物流
	public function getOrderLogistics($order_id){
	    $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_logistics  WHERE order_id='".(int)$order_id."'");

	    return $query->row;

	}
}